<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\admin\widget\Name;
use app\common\controller\ApiBaseController;
use app\common\enum\CreditCardStatus;
use app\common\enum\IsCreditCard;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\ext\IDb;
use app\common\ext\User;

class UserCreditCard extends ApiBaseController{

    /**
     * @功能：个人信用卡列表
     * @开发者：cxl
     */
    public function credit_card_list(){
        //接收参数
        $user_id = self::$uid;

        //判断信用卡是否存在
        $user_credit_card_where['is_delete'] = IsDelete::No;
        $user_credit_card_where['is_history'] = IsHistory::not;
        $user_credit_card_where['user_id'] = $user_id;
        $user_credit_card_list = IDb::getInstance("asset_credit_card")->setDbWhere($user_credit_card_where)->setDbOrder("user_credit_card_id")->sel();
        if(empty($user_credit_card_list) || !is_array($user_credit_card_list)){
            $user_credit_card_list = array();
        }

        $card_num = 0; // 卡数量
        $card_total_amount = 0; // 卡总额度
        $card_max_amount = 0; // 单卡最高额度
        $card_used_amount = 0; // 已使用总额度

        //取得用户银行卡所在用户
        foreach($user_credit_card_list as &$data){
            if(!empty($data['bank_id'])) $data['bank_name'] = Name::bank($data['bank_id']);

            if($data['amount'] > $card_max_amount){
                $card_max_amount = $data['amount'];
            }

            $card_num += 1;
            $card_total_amount = bcadd($card_total_amount,$data['amount'],2);
            $card_used_amount = bcadd($card_used_amount,$data['used_amount'],2);
        }

        //转化单位万
        $card_total_amount = bcdiv($card_total_amount,10000,2);
        $card_max_amount = bcdiv($card_max_amount,10000,2);
        $card_used_amount = bcdiv($card_used_amount,10000,2);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_credit_card_list",$user_credit_card_list);
        self::set_value("card_num",$card_num);
        self::set_value("card_total_amount",$card_total_amount);
        self::set_value("card_max_amount",$card_max_amount);
        self::set_value("card_used_amount",$card_used_amount);
        self::send();
    }

    /**
     * @功能：添加个人信用卡
     * @开发者：cxl
     */
    public function credit_card_add(){
        //接收参数
        $user_id = self::$uid;
        $bank_id  = self::get_data('bank_id',self::INT,'请选择信用卡发卡行');
        $amount  = self::get_data('amount');
        $used_amount  = self::get_data('used_amount');

        //判断额度
        if(intval($amount) <= 0){
            self::set_code(self::WARNING_0001);
            self::set_msg("请填写信用卡额度！");
            self::send();
        }

        //判断使用额度是否大于额度
        if(intval($amount) < intval($used_amount)){
            self::set_code(self::WARNING_0002);
            self::set_msg("使用额度不能大于信用卡额度！");
            self::send();
        }

        //长度限制
        $amount = self::data_filter($amount,self::INT,'信用卡额度');
        $used_amount = self::data_filter($used_amount,self::INT,'已使用额度');

        //添加信用卡
        $user_credit_card_data['user_id'] = $user_id;
        $user_credit_card_data['bank_id'] = $bank_id;
        $user_credit_card_data['amount'] = $amount;
        $user_credit_card_data['used_amount'] = $used_amount ?: 0;
        $user_credit_card_data['credit_card_status'] = CreditCardStatus::Wait;
        $user_credit_card_data['is_history'] = IsHistory::not;
        $user_credit_card_data['create_time'] = get_date_time();

        //开启事物
        IDb::dbStartTrans();

        //取得信用卡列表
        $user_credit_card_add = IDb::getInstance("asset_credit_card")->setDbData($user_credit_card_data)->add();

        //判断添加信用卡是否正常
        if($user_credit_card_add === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg("添加信用卡信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CREDIT_CARD,IsCreditCard::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("添加信用卡信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_credit_card_id",$user_credit_card_add);
        self::send();
    }

    /**
     * @功能：编辑个人信用卡
     * @开发者：cxl
     */
    public function credit_card_edit(){
        //接收参数
        $user_id = self::$uid;
        $user_credit_card_id  = self::get_data('user_credit_card_id',self::NOT_EMPTY,"信用卡编号不能为空！");
        $bank_id  = self::get_data('bank_id',self::INT,'请选择信用卡发卡行');
        $amount  = self::get_data('amount');
        $used_amount  = self::get_data('used_amount');

        //判断额度
        if(intval($amount) <= 0){
            self::set_code(self::WARNING_0001);
            self::set_msg("请填写信用卡额度！");
            self::send();
        }

        //判断使用额度是否大于额度
        if(intval($amount) < intval($used_amount)){
            self::set_code(self::WARNING_0002);
            self::set_msg("使用额度不能大于信用卡额度！");
            self::send();
        }

        //长度限制
        $amount = self::data_filter($amount,self::INT,'信用卡额度');
        $used_amount = self::data_filter($used_amount,self::INT,'已使用额度');

        //取得信用卡列表
//        $credit_card_where['user_id'] = $user_id;
        $credit_card_where['user_credit_card_id'] = $user_credit_card_id;
        $credit_card_info = IDb::getInstance("asset_credit_card")->setDbWhere($credit_card_where)->row();

        //判断是否审核通过
        if(empty($credit_card_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改信用卡信息失败！");
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        //判断是否审核通过
        if($credit_card_info['credit_card_status'] == CreditCardStatus::OK) {
            //取得信用卡列表
            $user_credit_card_data = null;
            $user_credit_card_where = null;
//            $user_credit_card_where['user_id'] = $user_id;
            $user_credit_card_where['user_credit_card_id'] = $user_credit_card_id;
            $user_credit_card_data['is_history'] = IsHistory::yes;
            $credit_card_upd = IDb::getInstance("asset_credit_card")->setDbWhere($user_credit_card_where)->setDbData($user_credit_card_data)->upd();
            if($credit_card_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("修改信用卡信息失败！");
                self::send();
            }

            //添加信用卡
            $user_credit_card_data = null;
            $user_credit_card_data['user_id'] = $user_id;
            $user_credit_card_data['bank_id'] = $bank_id;
            $user_credit_card_data['amount'] = $amount;
            $user_credit_card_data['used_amount'] = $used_amount ?: 0;
            $user_credit_card_data['credit_card_status'] = CreditCardStatus::Wait;
            $user_credit_card_data['is_history'] = IsHistory::not;
            $user_credit_card_data['create_time'] = get_date_time();
            $credit_card_add = IDb::getInstance("asset_credit_card")->setDbData($user_credit_card_data)->add();
            if($credit_card_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("修改信用卡信息失败！");
                self::send();
            }
        }else {
            //添加信用卡
            $user_credit_card_data = null;
            $user_credit_card_data['user_id'] = $user_id;
            $user_credit_card_data['bank_id'] = $bank_id;
            $user_credit_card_data['amount'] = $amount;
            $user_credit_card_data['used_amount'] = $used_amount ?: 0;
            $user_credit_card_data['credit_card_status'] = CreditCardStatus::Wait;
            $user_credit_card_data['is_history'] = IsHistory::not;
            $user_credit_card_data['update_time'] = get_date_time();

            //取得信用卡列表
            $user_credit_card_where = null;
            $user_credit_card_where['user_id'] = $user_id;
            $user_credit_card_where['user_credit_card_id'] = $user_credit_card_id;
            $user_credit_card_upd = IDb::getInstance("asset_credit_card")->setDbWhere($user_credit_card_where)->setDbData($user_credit_card_data)->upd();

            //判断修改信用卡是否正常
            if ($user_credit_card_upd === false) {
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改信用卡信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CREDIT_CARD,IsCreditCard::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("修改信用卡信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：删除个人信用卡
     * @开发者：cxl
     */
    public function credit_card_del(){
        //接收参数
        $user_id = self::$uid;
        $user_credit_card_id  = self::get_data('user_credit_card_id',self::NOT_EMPTY,"信用卡型号编码不能为空！");

        //取得信用卡列表
        $user_credit_card_where['user_id'] = $user_id;
        $user_credit_card_where['user_credit_card_id'] = $user_credit_card_id;
        $user_credit_card_data['is_delete'] = IsDelete::Yes;
        $user_credit_card_data['update_time'] = get_date_time();

        IDb::dbStartTrans();
        $user_credit_card_upd = IDb::getInstance("asset_credit_card")->setDbWhere($user_credit_card_where)->setDbData($user_credit_card_data)->upd();

        //判断修改信用卡是否正常
        if($user_credit_card_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("删除信用卡信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CREDIT_CARD);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步信用卡信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
